package blip.jcaf.cm;

import java.awt.Dimension;
import java.rmi.RemoteException;

import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

import blip.jcaf.cm.entity.Visitor;
import dk.pervasive.jcaf.ContextEvent;
import dk.pervasive.jcaf.EntityListener;
import dk.pervasive.jcaf.impl.RemoteEntityListenerImpl;
import dk.pervasive.jcaf.util.AbstractContextClient;

public class DisplayClient extends AbstractContextClient {
	
	private RemoteEntityListenerImpl listener1;
	private static String displayName;
	static JLabel welcome;
	static JLabel infoText1;
	static JLabel infoText2;
	static JLabel infoText3;

	public DisplayClient(String serviceUri, final String name) {
		super(serviceUri);
		displayName = name;
		run();
	}

    private void ping() {

    }
    
    /**
     * Adding entities to the ContextService
     */
    private void load() {
    	try {
			listener1 = new RemoteEntityListenerImpl();
			listener1.addEntityListener(new EntityListener() {
				
				@Override
				public void contextChanged(ContextEvent event) {
					if(event.getItem().toString().equals(displayName)) {
						if(event.getItem().toString() != "toBeDefined" && event.getRelationship() != null && event.getRelationship().toString() != null) {
							welcome.setText("Hello "+((Visitor) event.getEntity()).getName());
							infoText1.setText("Welcome to ITU!");
							infoText2.setText("You are at display "+event.getItem().toString());
							infoText3.setText("Your bluetooth ID is: "+event.getEntity().getId());
							System.out.println("Listener1: " + event.toString());
						} else {
							welcome.setText("");
							infoText1.setText("");
							infoText2.setText("");
							infoText3.setText("");
						}
					}
				}
			});
			
		} catch (RemoteException e) {
			e.printStackTrace();
		}
    }
    
    private void test() {
        try {
        	getContextService().addEntityListener(listener1, Visitor.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
	@Override
	public void run() {
		ping();
		load();
		test();
		createAndShowGUI();
		System.out.println(displayName);
	}
	
	private static void createAndShowGUI() {
        JFrame frame = new JFrame("Display "+displayName);
        frame.setSize(400, 200);
        frame.setMinimumSize(new Dimension(400,200));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        welcome = new JLabel("");
        infoText1 = new JLabel("");
        infoText2 = new JLabel("");
        infoText3 = new JLabel("");
        
        GroupLayout layout = new GroupLayout(frame.getContentPane());
        frame.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
        		layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        		.addGroup(layout.createSequentialGroup()
        				.addContainerGap()
        				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        						.addGroup(layout.createSequentialGroup()
        								.addComponent(welcome)
        								)
        								.addComponent(infoText1)
        								.addComponent(infoText2)
        								.addComponent(infoText3)
								)
        				)
        		);
        layout.setVerticalGroup(
        		layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        		.addGroup(layout.createSequentialGroup()
        				.addContainerGap()
        				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        						.addGroup(layout.createSequentialGroup()
        								.addComponent(welcome)
        								)
        						)
        						.addGap(20)
								.addGroup(layout.createSequentialGroup()
        								.addComponent(infoText1)
								)
								.addGroup(layout.createSequentialGroup()
        								.addComponent(infoText2)
								)
								.addGroup(layout.createSequentialGroup()
        								.addComponent(infoText3)
								)
        				)
        		);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

	public static void main(String[] args) {
		//new DisplayClient("blipTest", "itu.zone4.zone4c");
		/*javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });*/
		
		new DisplayClient(args[0],args[1]);
	}
}
